<?php

namespace App\Admin\Controllers;

use App\Models\InCode;

use Encore\Admin\Form;
use Encore\Admin\Form\Builder;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class InCodeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('邀请码');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

//    /**
//     * Edit interface.
//     *
//     * @param $id
//     * @return Content
//     */
//    public function edit($id)
//    {
//        return Admin::content(function (Content $content) use ($id) {
//
//            $content->header('邀请码');
//            $content->description('编辑');
//
//            $content->body($this->form()->edit($id));
//        });
//    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('邀请码');
            $content->description('创建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(InCode::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
//                $actions->disableDelete();
                $actions->disableEdit();
            });
//            $grid->id('ID')->sortable();
            $grid->model()->with('user');
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->like("code", "邀请码");
            });
            $grid->code("邀请码");
            $grid->user()->name("邀请人")->display(function ($name){
                if(!$name){
                    return "<span class='label label-primary'>平台</span>";
                } else {
//                    dd($this->user_id);
//                    dd($name);
                    $url = url("/admin/users/$this->user_id");
                    return "<a href=$url>$name</a>";
                }
            });

            $grid->status("使用状态")->display(function ($status) {
                $label = $status == 0 ? "未使用" : "已使用";
                return "<span class='label label-default'>$label</span>";
            });
            $grid->used()->name("使用者");
            $grid->created_at("创建时间")->sortable();
//            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(InCode::class, function (Form $form) {

            $form->display('id', 'ID');
//            $form->display("code",'邀请码');
            $form->number('number', '创建的数量');
//            $form->switch('user')
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    public function store(Request $request)
    {
        $number = $request->input("number", 0);
        if ($number >= 0) {

            $user_id = $request->input('user_id', 3);


            for ($i = 0; $i < $number; $i++) {
                $random = md5(random_int(1000, 4000) . time() . $user_id);
                $insert['code'] = $random;
                $insert['user_id'] = $user_id;
                $insert['status'] = 0;
                InCode::query()->create($insert);
            }


            admin_toastr("新增邀请码成功");
            $url = Input::get(Builder::PREVIOUS_URL_KEY) ?: $this->form->resource(0);
            return redirect($url);
//            return false;
        }
    }
}
